package uk.co.q3c.deplan.client.ui.component;


import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class CaptionGroup extends CaptionPanel {
	HorizontalFlowPanel hfp;

	/**
	 * 
	 */
	public CaptionGroup() {
		super();
		construct();
		layout();
	}

	/**
	 * @param caption
	 * @param asHTML
	 */
	public CaptionGroup(String caption, boolean asHTML) {
		super(caption, asHTML);
		construct();
		layout();
	}

	/**
	 * @param captionText
	 */
	public CaptionGroup(String captionText) {
		super(captionText);
		construct();
		layout();
	}

	private void construct() {
		hfp = new HorizontalFlowPanel();
	}

	private void layout() {
		super.add(hfp);
		hfp.setWidth("100%");
	}

	/**
	 * Adds the field and associated label. This was also going to calculate yhe width of the caption panel but
	 * {@link Widget#getOffsetWidth() is not available (in FF at least) until after the page has loaded}
	 * 
	 * @param field
	 * @param name
	 */
	public void addField(Widget field, String name) {
		Label label = new Label(name);
		addField(field, label);
	}

	public void addField(Widget field, Label label) {
		label.addStyleName("fieldLabel");
		hfp.add(label);
		hfp.add(field);
	}

	@Override
	public void add(Widget w) {
		hfp.add(w);
	}

	public HorizontalFlowPanel group() {
		return hfp;

	}

	public void addField(Widget field, Label beforeLabel, Label afterLabel) {
		beforeLabel.addStyleName("fieldLabel");
		afterLabel.addStyleName("fieldLabel");
		hfp.add(beforeLabel);
		hfp.add(field);
		hfp.add(afterLabel);

	}

}
